package com.wjr.braveheart.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import static android.os.Environment.MEDIA_MOUNTED;

public class BitmapUtils {
    /**
     * Bitmap转Byte
     *
     * @param bitmap
     * @return
     */
    public static byte[] bitmapTobyte(Bitmap bitmap) {
        //实例化字节数组输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);//压缩位图
        return baos.toByteArray();//创建分配字节数组
    }

    /**
     * Byte转Bitmap
     *
     * @param data
     * @return
     */
    public static Bitmap byteTobitmap(byte[] data) {
        return BitmapFactory.decodeByteArray(data, 0, data.length);//从字节数组解码位图
    }

    /**
     * 将Bitmap转换成字符串
     *
     * @param bitmap
     * @return
     */
    public static String bitmapToString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

    /**
     * 将字符串转换成Bitmap类型
     *
     * @param string
     * @return
     */
    public static Bitmap stringToBitmap(String string) {
        // 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    /**
     * 生成APP专属文件
     * 如果是路径一则不是APP专属文件
     *
     * @param bitmap
     * @return
     */
    public static File bitmapToFile(Context context, Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
        String filePath = "";
        if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {//判断外部存储是否可用
            //外部存储路径下
            //路径一/storage/emulated/0/1572935172507_signature.png
//            filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + System.currentTimeMillis() + "_signature.png";
            //外部存储APP专属文件路径下
            //路径二/storage/emulated/0/Android/data/com.weiyi.vsystem/files/signature/1572935255318_signature.png 外部存储APP专属文件路径下
            filePath = context.getExternalFilesDir("signature").getAbsolutePath() + File.separator + System.currentTimeMillis() + "_signature.png";

        } else {//没外部存储就使用内部存储
            //内部存储APP专属文件路径下
            //内部存储/data/user/0/com.weiyi.vsystem/files/1572934860787_signature.png
            filePath = context.getFilesDir().getAbsolutePath() + File.separator + System.currentTimeMillis() + "_signature.png";
        }
//        LogUtils.e(filePath);
        File file = new File(filePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            InputStream is = new ByteArrayInputStream(baos.toByteArray());
            int x = 0;
            byte[] b = new byte[1024 * 100];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
}
